package serverlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connectDB.AccountDB;
import entities.Accountentities;


/**
 * Servlet implementation class Logincontrol
 */
@WebServlet("/Logincontrol")
public class Logincontrol extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Logincontrol() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doprocess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doprocess(request,response);
		
	}

	private void doprocess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

//
response.setContentType("text/html");
		
		//connectDB.AccountDB accDB = new AccountDB();
		AccountDB accDb = new AccountDB();
		Accountentities acc = new Accountentities(request.getParameter("username"), "","",0, "","", "", "", "", 0, request.getParameter("password"));
		
		
		
		if(accDb.checkAccount(acc))
		{
			  
				ServletContext context = getServletContext();
				request.setAttribute("newUser", acc);
				RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp");
				dispatcher.forward(request, response);
				return;
		}
		else
		{
			response.sendRedirect("login.jsp");
			return;
		}
		
		//
		
	}

}
